class Details{
    constructor(){
        this.details = document.querySelector('.details')
        this.smallPic = document.querySelector('.smallBox').firstElementChild
        this.bigPic = document.querySelector('.bigBox').firstElementChild
        this.title = document.querySelector('.title')
        this.price = document.querySelector('.price')
        
        this.init()
    }
    init(){
        this.request()
    }
    getQueryStr(id){
        let str = location.search.slice(1)
        let arr = str.split('&')
        let obj = {}
        arr.forEach(item=>{
            let newArr = item.split('=')
            obj[newArr[0]] = newArr[1] 
        })
        return obj[id]
    }
    request(){
        let id = this.getQueryStr('id') - 0
        if(!id){
            location.href = '../index.html'
            return false
        }else{
            pAjax({url: '../libs/goods.json'})
            .then(res=>{
                let data = JSON.parse(res)
                data = data.slice(24, 48)
                this.render(id, data)
            })
        }
    }
    render(id, data){
        let res = data.find(item=>{return item.goods_id == id})
        console.log(res)
        this.details.innerHTML = res.goods_introduce
        this.smallPic.src = res.img_big_logo
        this.bigPic.src = res.img_small_logo
        this.title.innerHTML = `${res.title}`
        this.price.innerHTML = `${res.price}`
    }
}
new Details()